# 通过正则表达式筛选虎牙直播主播头像，主播名，直播间封面
import re
import requests
from fake_useragent import FakeUserAgent

# 定义请求头，发起网络请求
headers = {'User-Agent': FakeUserAgent().random}
url = 'https://www.huya.com/g/2633'
response = requests.get(url, headers=headers)

main_info = response.text

# 分别筛选出主播名，封面图链接，主播头像链接
cover_image_rule = r'"sScreenshot":"(.*?)","iIsSecret"'
head_image_rule = r'"sAvatar180":"(.*?)","lProfileRoom"'
name_rule = r'"sNick":"(.*?)","iSex"'
cover_image = re.findall(cover_image_rule, main_info)
head_image = re.findall(head_image_rule, main_info)
names = re.findall(name_rule, main_info)

# 通过for循环和zip来进行数据的规整
for name, cover, head in zip(names, cover_image, head_image):
    print('主播名为：'+name)
    print('封面图片链接：'+cover)
    print('主播头像链接：'+head)
    print('======================================================================')
